<template>
  <el-dialog v-model="dialogVisible" :title="title" width="30%" :before-close="handleClose">
    <div class="tab4-dialog-hetong">
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="130px">
        <el-form-item prop="customer_id" label="客户名称">
          <el-input placeholder="客户名称" disabled v-model="dataInfo.name" />
        </el-form-item>
        <el-form-item prop="order_no" label="合同编号">
          <el-select style="width: 100%" v-model="ruleForm.order_no" placeholder="合同编号" filterable clearable>
            <el-option v-for="item in contractOptions" :key="item.id" :label="item.order_no" :value="item.order_no" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item prop="order_no" label="合同编号">
          <el-input placeholder="合同编号" disabled v-model="ruleForm.order_no" />
        </el-form-item> -->


        <el-form-item label="回款日期" prop="return_date">
          <el-date-picker style="width: 100%" v-model="ruleForm.return_date" type="date" placeholder="回款日期"
            value-format="YYYY-MM-DD" />
        </el-form-item>


        <el-form-item prop="return_money" label="回款金额(元)">
          <el-input placeholder="回款金额（元）" v-model="ruleForm.return_money" />
        </el-form-item>


        <el-form-item prop="" label="附件" style="width: 100%">
          <el-upload class="upload-demo" action="#" :auto-upload="false" :on-change="fileOnChange"
            :show-file-list="false">
            <div class="file-btn">
              <el-button type="primary" size="small"> 文件上传 </el-button>
            </div>
          </el-upload>
          <div class="file-list" v-if="fileList.length > 0" style="width: 100%">
            <div class="file-list-item" v-for="(item, i) in fileList">
              <span> {{ item.name }}</span>
              <el-icon class="close" @click="fileRemove(i)">
                <IEpClose />
              </el-icon>
            </div>
          </div>
        </el-form-item>

        <el-form-item prop="remark" label="备注" style="width: 100%">
          <el-input placeholder="备注" type="textarea" :rows="2" v-model="ruleForm.remark" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { fileApi } from "@/plugins/api/upload";
import { optionsApi as contractApi } from "@/plugins/api/customer/manage/tab4";
import { receivablesCreateApi } from "@/plugins/api/customer/manage/tab6";
const props = defineProps({
  dataInfo: {
    type: Object,
    default: {},
  },

  // 公司签署人
  userOptions: {
    type: Array,
    default: [],
  },
  // 客户签署人
  userData: {
    type: Array,
    default: [],
  },
});
let ruleForm = ref({});
let rules = {
  order_no: [
    {
      required: true,
      message: "合同编号",
      trigger: "change",
    },
  ],
  return_date: [
    {
      required: true,
      message: "回款日期",
      trigger: "blur",
    },
  ],
  return_money: [
    {
      required: true,
      message: "回款金额",
      trigger: "blur",
    },
  ],
};
let dialogVisible = ref(false);
function handleClose(done) {
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => { });
}
let title = ref("");
let submitType = ref("");
let ruleFormRef = ref(null);
function initFun(row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  if (row) {
    title.value = '编辑'
  } else {
    title.value = '新增'
  }
  ruleForm.value = {}
  fileList.value = []; //清空文件列表
  dialogVisible.value = true;
  getContract()
}
// 相关合同
let contractOptions = ref([]);
function getContract() {
  contractApi({
    customer_id: props.dataInfo.id,
  }).then((data) => {
    contractOptions.value = data;
  });
}
const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      let obj = { ...ruleForm.value }
      obj.customer_id = props.dataInfo.id;
      obj.files = fileList.value
      receivablesCreateApi(obj).then((res) => {
        ElMessage.success("新增成功！");
        dialogVisible.value = false;
        emit("refreshList");
      });
    }
  });
}

// 文件上传
let fileList = ref([]);
function fileOnChange(file) {
  let formData = new FormData();
  formData.append("file", file.raw);
  fileApi(formData).then((resp) => {
    if (resp) {
      fileList.value.push({
        name: resp.file_name,
        url: resp.file_url,
      });
    } else {
      ElMessage.danger("上传失败！请重新上传！");
    }
  });
}
function fileRemove(i) {
  fileList.value.splice(i, 1)
}

onMounted(() => { });
defineExpose({
  initFun,
});
</script>
<style scoped lang="scss">
.tab4-dialog-hetong {
  h4 {
    margin: 0;
    margin-bottom: 20px;
  }

  :deep(.el-form-item) {

    margin-right: 0;
  }

  .goods-head {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;

    span {
      margin-right: 20px;
    }
  }

  .file-list {
    .file-list-item {
      display: flex;

      span,
      .close {
        align-self: center;
      }

      .close {
        margin-left: 20px;

        &:hover {
          cursor: pointer;
          color: rgb(26, 145, 236);
        }
      }
    }
  }
}
</style>
